<?php
namespace helper;
class Str {
	// 随机字符串
	public static function rand(int $length=10,$options='l'){
		$chars = '';
		if (stripos($options, 'l') !== false) {
			$chars.= 'abcdefghijklmnopqrstuvwxyz';
		}
		if (stripos($options, 'u') !== false) {
			$chars.= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		}
		if (stripos($options, 'n') !== false) {
			$chars.= '0123456789';
		}
		$result = '';
		$charLength = strlen($chars);
		$length = intval($length);
		if ($length < 1) {
			$length = 1;
		}
		for ($i = 0; $i < $length; $i++) {
			$index = rand(0, $charLength - 1);
			$result .= $chars[$index];
		}
		return $result;
	}
	// 字符集转换
	public static function iconv($value,$toEnc='UTF-8',$fromEnc='UTF-8,GBK,BIG5'){
		static $alias=[
			'gb2312'=>'GBK',
			'gbk'=>'GBK',
			'big5'=>'BIG5',
			'utf-8'=>'UTF-8',
			'utf8'=>'UTF-8',
			'utf-16'=>'UTF-16',
			'utf16'=>'UTF-16',
			'utf-32'=>'UTF-32',
			'utf32'=>'UTF-32',
			'ascii'=>'ASCII',
			'unicode'=>'UNICODE',
		];
		static $mb=null;
		if(is_null($mb)){
			$mb=function_exists('mb_convert_encoding');
		}
		if($value and (is_array($value) or is_object($value) or is_string($value))){
			$fromEnc=$alias[$fromEnc]?$alias[$fromEnc]:strtoupper($fromEnc);
			$toEnc=$alias[$toEnc]?$alias[$toEnc]:strtoupper($toEnc);
			// 源字符集和目标字符集相同时直接返回
			if($fromEnc==$toEnc){
				return $value;
			}
			if(is_array($value)){
				foreach($value as $k => $v){
					unset($value[$k]);
					$k = self::iconv($k,$toEnc,$fromEnc);
					$value[$k] = self::iconv($v,$toEnc,$fromEnc);
				}
				return $value;
			}elseif(is_object($value)){
				foreach($value as $k => $v){
					unset($value->$k);
					$k = self::iconv($k,$toEnc,$fromEnc);
					$value->$k = self::iconv($v,$toEnc,$fromEnc);
				}
				return $value;
			}elseif(is_string($value)){
				// 如果可有开启mbstring扩展 就使用iconv函数进行转换
				if(!$mb){
					return iconv($fromEnc,"{$toEnc}//IGNORE",$value);
				}
				// //mbstring扩展转换的源字符集支持更多
				// $chars=['UTF-8','GBK','GB2312','BIG5'];
				// if(!in_array($fromEnc,$chars)){
				// 	$chars[]=$fromEnc;
				// }
				// $fromEnc=implode(',',$chars);
				return mb_convert_encoding($value,$toEnc,$fromEnc);
			}
		}
		return $value;
	}
	
	public static function endsWith($string, $ending) {
		$length = strlen($ending);
		if ($length == 0) {
			return 0;
		}
		return substr($string, -$length) === $ending;
	}

	// 替换全部
	public static function replace($find,$to,$str){
		while(strpos($str,$find)!==false){
			$str=str_replace($find,$to,$str);
		}
		return $str;
	}
	// 不区分大小写替换全部
	public static function iReplace($find,$to,$str){
		while(stripos($str,$find)!==false){
			$str=str_ireplace($find,$to,$str);
		}
		return $str;
	}
	// 转大驼峰 ConvStringTo
	public static function studly($str){
		return str_replace(' ','',ucwords(str_replace(['_','-'],' ',$str)));
	}
	// 转小驼峰 convStringTo
	public static function camel($str){
		return lcfirst(self::studly($str));
	}
	// 转下划线 conv_string_to
	public static function snake($str){
		return strtolower(preg_replace('/[A-Z]/','_$0', self::camel($str)));
	}
	// 转下划线大写 CONV_STRING_TO
	public static function snakeUpper($str){
		return strtoupper(preg_replace('/[A-Z]/','_$0', self::camel($str)));
	}
	// 转减号线 conv-string-to
	public static function hyphen($str){
		return strtolower(preg_replace('/[A-Z]/', '-$0', self::camel($str)));
	}
	public static function toByte($input){
		preg_match('/([\d\.]+)(\w*)/',$input,$matches);
		$type=strtolower($matches[2]);
		switch($type){
		case "b":
			$output=$matches[1];
			break;
		case "kb":
		case "k":
			$output=$matches[1]*1024;
			break;
		case "mb":
		case "m":
			$output=$matches[1]*1024*1024;
			break;
		case "gb":
		case "g":
			$output=$matches[1]*1024*1024*1024;
			break;
		case "tb":
		case "t":
			$output=$matches[1]*1024*1024*1024*1024;
			break;
		default:
			$output=$matches[1];
		}
		return is_numeric($output)?$output:((int)$matches[1]);
	}
	function formatByte($size) {
		$units=array('B','KB','MB','GB','TB');
		for($i=0; $size>=1024 and $i<4; $i++){
			$size /= 1024;
		}
		return round($size,2).$units[$i];
	}
}